package com.digital.community;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.digital.community.common.utils.ConnectWeb;
import com.digital.community.common.utils.HttpUtils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

public class TBGetAttach extends Activity {
	String attachs = "";
	ImageView imageView1,imageView2,imageView3;
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.tbtask_showattach);
        imageView1 = (ImageView) findViewById(R.id.image_attach1);
        imageView2 = (ImageView) findViewById(R.id.image_attach2);
        imageView3 = (ImageView) findViewById(R.id.image_attach3);
    }
    
    public void onResume(){
    	super.onResume();
    	attachs = getIntent().getStringExtra("tattach");
    	if(attachs.equals("")){
    		return;
    	}
    	String[] paths = attachs.split("||");
    	
        byte[] data1;
        byte[] data2;
        byte[] data3;
		try {
			data1 = getImage(paths[0]);
	        data2 = getImage(paths[1]);
	        data3 = getImage(paths[3]);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(this, "查看出错！", Toast.LENGTH_SHORT);
			finish();
			return;
		}
//        byte[] data4 = getImage(paths[4]);
        Bitmap bitmap1 = BitmapFactory.decodeByteArray(data1, 0, data1.length);
        Bitmap bitmap2 = BitmapFactory.decodeByteArray(data2, 0, data2.length);
        Bitmap bitmap3 = BitmapFactory.decodeByteArray(data3, 0, data3.length);
//        Bitmap bitmap1 = BitmapFactory.decodeByteArray(data, 0, data.length);
        imageView1.setImageBitmap(bitmap1);

    }
    
    
    
    
    public static byte[] getImage(String path) throws Exception {

        URL url = new URL(HttpUtils.DEFAULT_URL+path);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");

        conn.setConnectTimeout(5 * 1000);

        InputStream inStream = conn.getInputStream();

        return readFromInput(inStream);

     }

  

     public static byte[] readFromInput(InputStream inStream) throws Exception {

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];

        int len = 0;

        while ((len = inStream.read(buffer)) != -1) {

            outStream.write(buffer, 0, len);

        }

        inStream.close();

        return outStream.toByteArray();

     }

}
